<<<<<<< HEAD
package com.hsx.servlet;

import org.w3c.dom.css.RGBColor;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

public class ImageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //浏览器没三秒刷新一次
        resp.setHeader("refresh","3");
        //new一个BufferedImage 涉资宽度80，高20，颜色为三色图RGB
        BufferedImage image = new BufferedImage(80,20, BufferedImage.TYPE_INT_RGB);
        //得到图片
        Graphics2D g = (Graphics2D) image.getGraphics();
        //设置图片背景色
        g.setColor(Color.white);
        g.fillRect(0,0,80,20);
        //给图片写数据，吧验证码画上去
        g.setColor(Color.BLUE);
        g.setFont(new Font(null, Font.BOLD,20));
        g.drawString(makeNum(),0,20);
        //告诉浏览器这个请求用图片的方式打开
        resp.setContentType("image/png");
        //告诉浏览器不要缓存
        resp.setDateHeader("expires",-1);
        resp.setHeader("Cache-Control","no-cache");
        resp.setHeader("Pragma","no-cache");
        ImageIO.write(image, "jpg", resp.getOutputStream());
    }
    public String makeNum(){
        Random random = new Random();
        String num = random.nextInt(999999) + "";
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i <7-num.length() ; i++) {
            sb.append("0");
        }
         num= sb.toString() + num;
        return num;
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
=======
package com.hsx.servlet;

import org.w3c.dom.css.RGBColor;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

public class ImageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //浏览器没三秒刷新一次
        resp.setHeader("refresh","3");
        //new一个BufferedImage 涉资宽度80，高20，颜色为三色图RGB
        BufferedImage image = new BufferedImage(80,20, BufferedImage.TYPE_INT_RGB);
        //得到图片
        Graphics2D g = (Graphics2D) image.getGraphics();
        //设置图片背景色
        g.setColor(Color.white);
        g.fillRect(0,0,80,20);
        //给图片写数据，吧验证码画上去
        g.setColor(Color.BLUE);
        g.setFont(new Font(null, Font.BOLD,20));
        g.drawString(makeNum(),0,20);
        //告诉浏览器这个请求用图片的方式打开
        resp.setContentType("image/png");
        //告诉浏览器不要缓存
        resp.setDateHeader("expires",-1);
        resp.setHeader("Cache-Control","no-cache");
        resp.setHeader("Pragma","no-cache");
        ImageIO.write(image, "jpg", resp.getOutputStream());
    }
    public String makeNum(){
        Random random = new Random();
        String num = random.nextInt(999999) + "";
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i <7-num.length() ; i++) {
            sb.append("0");
        }
         num= sb.toString() + num;
        return num;
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
>>>>>>> 0955e43271ec003176a75da3f24c6d9c0071fe9b
